<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
  <!-- 引入样式 -->
  <link rel="stylesheet" href="../../plugins/element-ui/index.css" />
  <link rel="stylesheet" href="../../styles/common.css" />
  <link rel="stylesheet" href="../../styles/page.css" />
</head>
<body>
  <div class="dashboard-container" id="category-app">
    <div class="container">
      <div
        class="tableBar"
        style="display: inline-block"
      >
        <el-button
          type="primary"
          @click="addClass()"
        >
          + 新增
        </el-button>
      </div>
      <el-table
        :data="tableData"
        stripe
        class="tableBox"
      >
        <el-table-column
          prop="id"
          label="学校id"
        /></el-table-column>
        <el-table-column
          prop="name"
          label="专业名称"
        ></el-table-column>
        <el-table-column
                prop="address"
                label="学校地址"
        ></el-table-column>
        <el-table-column
                prop="collegeName"
                label="学校名称"
        ></el-table-column>
        <el-table-column
                prop="level1"
                label="专业大类"
        ></el-table-column>
        <el-table-column
                prop="level2"
                label="专业二类"
        ></el-table-column>
        <el-table-column
                prop="mId"
                label="专业id"
        ></el-table-column>
        <el-table-column
                prop="mNum"
                label="专业人数"
        ></el-table-column>
        <el-table-column
          label="操作"
          width="160"
          align="center"
        >
          <template slot-scope="scope">
            <el-button
              type="text"
              size="small"
              class="blueBug"
              @click="editHandle(scope.row)"
            >
              修改
            </el-button>
            <el-button
              type="text"
              size="small"
              class="delBut non"
              @click="deleteHandle(scope.row.mId)"
            >
              删除
            </el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        class="pageList"
        :page-sizes="[10, 20, 30, 40]"
        :page-size="pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="counts"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      ></el-pagination>
    </div>
    <el-dialog
      :title="classData.title"
      :visible.sync="classData.dialogVisible"
      width="30%"
      :before-close="handleClose"
    >
      <el-form
        class="demo-form-inline"
        label-width="100px"
      >
        <el-form-item label="学校id：">
          <el-input
            v-model="classData.id"
            placeholder="请输入学校id"
            maxlength="14"
          />
        </el-form-item>
        <el-form-item label="专业id：">
          <el-input
                  v-model="classData.mId"
                  placeholder="请输入专业id"
                  maxlength="14"
          />
        </el-form-item>
        <el-form-item label="专业大类：">
          <el-input
                  v-model="classData.level1"
                  placeholder="请输入专业大类"
                  maxlength="14"
          />
        </el-form-item>
        <el-form-item label="专业二类：">
          <el-input
                  v-model="classData.level2"
                  placeholder="请输入专业二类"
                  maxlength="14"
          />
        </el-form-item>
        <el-form-item label="专业名称：">
          <el-input
                  v-model="classData.name"
                  placeholder="请输入专业名称"
                  maxlength="14"
          />
        </el-form-item>
        <el-form-item label="专业人数：">
          <el-input
                  v-model="classData.mNum"
                  placeholder="请输入专业人数"
                  maxlength="14"
          />
        </el-form-item>
      </el-form>
      <span
        slot="footer"
        class="dialog-footer"
      >
        <el-button
          size="medium"
          @click="classData.dialogVisible = false"
        >取 消</el-button>
        <el-button
          type="primary"
          size="medium"
          @click="submitForm()"
        >确 定</el-button>
        <!--<el-button
          v-if="action != 'edit'"
          type="primary"
          size="medium"
          class="continue"
          @click="submitForm('go')"
        > 保存并继续添加 </el-button>-->
      </span>
    </el-dialog>
  </div>
    <!-- 开发环境版本，包含了有帮助的命令行警告 -->
    <script src="../../plugins/vue/vue.js"></script>
    <!-- 引入组件库 -->
    <script src="../../plugins/element-ui/index.js"></script>
    <!-- 引入axios -->
    <script src="../../plugins/axios/axios.min.js"></script>
    <script src="../../js/request.js"></script>
    <script src="../../api/category.js"></script>
    <script>
      new Vue({
        el: '#category-app',
        data() {
          return {
            action: '',
            counts: 0,
            page: 1,
            pageSize: 10,
            tableData: [],
            type :'',
            classData: {
              'title': '添加专业',
              'dialogVisible': false,
              'id': '',
              'name': '',
              'level1':'',
              'level2':'',
              'mNum':'',
              'mId':''

            }
          }
        },
        computed: {},
        created() {
          this.init()
        },
        mounted() {
        },
        methods: {
          async init () {
            await getCategoryPage({'page': this.page, 'pageSize': this.pageSize}).then(res => {
              if (String(res.code) === '200') {
                this.tableData = res.data
                this.counts = Number(res.count)
              } else {
                this.$message.error(res.msg || '操作失败')
              }
            }).catch(err => {
              this.$message.error('请求出错了：' + err)
            })
          },
          handleQuery() {
            this.page = 1;
            this.init();
          },
           // 添加
          addClass(st) {

            this.action = 'add'
            this.classData.name = ''
            this.classData.level1 = ''
            this.classData.id = ''
            this.classData.level2 = ''
            this.classData.mId = ''
            this.classData.mNum = ''
            this.classData.dialogVisible = true
          },
          editHandle(dat) {
            this.classData.title = '修改分类'
            this.action = 'edit'
            this.classData.name = dat.name
            this.classData.mId = dat.mId
            this.classData.level1 = dat.level1
            this.classData.id = dat.id
            this.classData.level2 = dat.level2
            this.classData.mNum = dat.mNum
            this.classData.dialogVisible = true
          },
          // 关闭弹窗
          handleClose(params) {
            this.classData.dialogVisible = false
          },
          //删除
          deleteHandle(mId) {
            this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
              'confirmButtonText': '确定',
              'cancelButtonText': '取消',
              'type': 'warning'
            }).then(() => {
              deleCategory(mId).then(res => {
                if (res.code === 200) {

                  this.$message.success('删除成功！')
                  this.handleQuery()
                } else {
                  this.$message.error(res.msg || '操作失败')
                }
              }).catch(err => {
                this.$message.error('请求出错了：' + err)
              })
            })
          },
          //数据提交
          submitForm(st) {
              const classData = this.classData
              const valid = (classData.name === 0 ||classData.name)  && (classData.id === 0 || classData.id)&& (classData.level1 === 0 || classData.level1)&& (classData.level2 === 0 || classData.level2)
              if (this.action === 'add') {
                if (valid) {
                  const reg = /^\d+$/
                  var major=this.classData
                    addCategory({'id':this.classData.id,'name': this.classData.name,'mId': this.classData.mId,'level1': this.classData.level1,'level2': this.classData.level2,'mNum': this.classData.mNum}).then(res => {

                      // if (res.code === 200) {
                        this.$message.success('专业添加成功！')
                        //TODO 专业添加成功后需要将弹框关闭

                        if (!st) {
                          this.classData.dialogVisible = false
                        } else {
                          this.classData.name = ''
                          this.classData.mId = ''
                          this.classData.level1 = ''
                          this.classData.id = ''
                          this.classData.level2 = ''
                          this.classData.mNum = ''
                        }
                      this.$message.success('添加成功！')
                      this.handleClose()
                      this.handleQuery()
                      // } else {
                      //   this.$message.error(res.msg || '操作失败')
                      // }
                    }).catch(err => {
                      this.$message.error('请求出错了：' + err)
                    })

                  
              } else {
                  this.$message.error('输入框数据不能为空')
              }
            } else if (valid) {
              const reg = /^\d+$/

                editCategory({'id':this.classData.id,'name': this.classData.name,'level1': this.classData.level1,'level2': this.classData.level2,'mNum': this.classData.mNum,'mId': this.classData.mId}).then(res => {
                  // if (res.data === 200) {
                    this.$message.success('专业修改成功！')

                    this.classData.dialogVisible = false
                    this.handleQuery()

                  this.handleClose()


                  // } else {
                  //   this.$message.error(res.msg || '操作失败')
                  // }
                }).catch(err => {
                  this.$message.error('请求出错了：' + err)
                })

            } else {
              this.$message.error('输入框数据不能为空')
            }
          },
          handleSizeChange (val) {
            this.pageSize = val
            this.init()
          },
          handleCurrentChange (val) {
            this.page = val
            this.init()
          }
        }
      })
    </script>
</body>
</html>